<?php

declare(strict_types=1);
namespace ImiApp\ApiServer\Api\Controller\store;

use Imi\Db\Db;
use Imi\Server\Http\Route\Annotation\Action;
use Imi\Server\Http\Route\Annotation\Controller;
use Imi\Server\Http\Route\Annotation\Route;
use Psr\Http\Message\ResponseInterface;
use Imi\Aop\Annotation\Inject;
// use Phpben\Imi\Validate\Annotation\Validate;
use Phpben\Imi\Auth\Annotation\Auth;
use Imi\Config;

/**
 * 测试继承店铺接口💻
 * @Auth(name="token",nologin={"no"})
 * @Controller("/store.store_test/")
 */
class StoreTest extends StoreOwner
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['no'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];


    /**
     * 测试发布商品
     *
     * @return void
     */
    protected function testAdd($title = '发布商品', $run_num = 1)
    {
        $s = $this->getMillisecond();
        for ($i = 0; $i < $run_num; $i++) {
          
            $this->TestAddGoods();
        }
        $e = $this->getMillisecond();
        echo $title, '运行时间', $e - $s, '(ms);', (($e - $s) / 1000), '秒\n';
    }

    /**
     * 测试添加商品
     *
     * @return void
     * @Action
    */
    public function TestAddGoods()
    {
        $data['goodscate_ids'] = '168';
        $data['goodsbrand_id'] = 4;
        $data['content'] = '<p><font color="#000000" style="background-color: rgb(255, 255, 0);">图文<b>详情</b></font></p><p><br></p><p><img src="http://api.pddds.cn/uploads/20230330/90e3e20f452924c4dd7f797928321e49.png" style="width: 405px;"></p>';
        $data['dispatch_ids'] = '1'; //发货模板
        $data['dispatch_type'] = 'express'; //发货方式:express=物流快递,selfetch=用户自提,store=商家配送,autosend=自动发货,service=上门核销',
        $data['expire_day'] = '0';
        $data['image'] = '/uploads/20230330/90e3e20f452924c4dd7f797928321e49.png';
        $data['images'] = '/uploads/20230330/90e3e20f452924c4dd7f797928321e49.png,/uploads/20230322/e1713bfc3a72fca377bddd460818f3b8.png';
        $data['is_sku'] = '1'; //是否多规格 0=否1=是
        $data['original_price'] = '988.00'; //原价
        $data['params'] = '[{"title":"品牌","content":"xiaomi"},{"title":"产地","content":"中国"}]';
        // $data['params_arr][0][title'] = '品牌';
        // $data['params_arr][0][content'] = 'iphone';
        // $data['params_arr][1][title'] = '产地';
        // $data['params_arr][1][content'] = '中国';
        $data['price'] = '0.01'; //价格
        $data['service_ids'] = '4,3,2'; //服务标签
        $data['show_sales'] = '2'; //显示销量
        $data['status'] = 'up';
        $data['subtitle'] = '测试单元商品';
        $data['title'] = '测试单元商品';
        $data['type'] = 'normal';
        $data['views'] = '10'; //浏览人数
        $data['weigh'] = '11';
        $data['weight'] = '';
        $data['stock'] = '';
        $data['stock_warning_switch'] = 'false';
        $data['sn'] = '';
        $data['autosend_content'] = '';
        $data['store_id'] = 1; //店铺ID
        $data['createtime'] = time();
        $data['updatetime'] = time();
        // $data['goods_service_ratio'] = 1;//卖家服务费比率
  


        Db::getInstance()->beginTransaction();

        try {
            $data['goods_service_ratio'] = $this->StoreGetGoodsMoneyServerRatio($data, 12, 1);

            $data['id'] = Db::name('goods')->strict(false)->insert($data)->getLastInsertId();
            $sku['listData'] = '[{"id":138,"name":"测试颜色","pid":0,"goods_id":40,"weigh":0,"children":[{"id":139,"name":"测试黑色","pid":138,"goods_id":40,"weigh":0,"temp_id":2},{"id":140,"name":"白色","pid":138,"goods_id":40,"weigh":0,"temp_id":3}],"temp_id":1},{"id":141,"name":"容量","pid":0,"goods_id":40,"weigh":0,"children":[{"id":142,"name":"256G","pid":141,"goods_id":40,"weigh":0,"temp_id":5},{"id":143,"name":"128G","pid":141,"goods_id":40,"weigh":0,"temp_id":6}],"temp_id":4}]';
            $sku['priceData'] = '[{"id":141,"goods_sku_ids":"139,142","goods_id":40,"weigh":0,"image":"","stock":10,"stock_warning":null,"sales":0,"sn":"A1","weight":160,"price":"1.00","goods_sku_text":["测试黑色","256G"],"status":"up","createtime":1680575318,"updatetime":1680575318,"deletetime":null,"temp_id":1,"goods_sku_temp_ids":[2,5]},{"id":142,"goods_sku_ids":"140,142","goods_id":40,"weigh":0,"image":"","stock":20,"stock_warning":null,"sales":0,"sn":"A2","weight":160,"price":"2.00","goods_sku_text":["白色","256G"],"status":"up","createtime":1680575318,"updatetime":1680575318,"deletetime":null,"temp_id":2,"goods_sku_temp_ids":[3,5]},{"id":143,"goods_sku_ids":"139,143","goods_id":40,"weigh":0,"image":"","stock":30,"stock_warning":null,"sales":0,"sn":"A3","weight":160,"price":"3.00","goods_sku_text":["黑色","128G"],"status":"up","createtime":1680575318,"updatetime":1680575318,"deletetime":null,"temp_id":3,"goods_sku_temp_ids":[2,6]},{"id":144,"goods_sku_ids":"140,143","goods_id":40,"weigh":0,"image":"","stock":4,"stock_warning":null,"sales":0,"sn":"A4","weight":160,"price":"4.00","goods_sku_text":["白色","128G"],"status":"down","createtime":1680575318,"updatetime":1680575318,"deletetime":null,"temp_id":4,"goods_sku_temp_ids":[3,6]}]';

            $this->editSku($data, $sku, 'add');

            Db::getInstance()->commit();
        } catch (\Throwable $e) {
            Db::getInstance()->rollBack();
            return $this->error($e->getMessage());
        }

    }



}
